package com.imooc.quartzDemo.scheduler;

import com.imooc.quartzDemo.job.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloScheduler {
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("current time:"+ format.format(date));
       /* date.setTime(date.getTime()+4000);
        Date endDate=new Date();
        endDate.setTime(endDate.getTime()+6000);*/


        JobDetail jobDetail=JobBuilder.newJob(HelloJob.class).
                withIdentity("myjob","group1")
                .build();
        //1.2017年内每天10点15分触发一次
        //  0 15 10 ? * * 2017
        //2.每天的14点整至14点59分55秒,以及18点整至18点59分55秒,每5秒钟触发一次.
        // 0/5 * 14,18 * * ?
        //3.每月周一至周五的10点15分触发一次
        // 0 15 10 * ? MON-FRI
        //4.每月最后一天的10点15分触发一次
        // 0 15 10 L * ?
        //5.每月第三个周五 10点15分触发一次
        // 0 15 10 ? * 6#3
        CronTrigger trigger=(CronTrigger)TriggerBuilder.newTrigger()
                .withIdentity("myTrigger","group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *"))
                .build();

        SchedulerFactory schedulerFactory=new StdSchedulerFactory();
        Scheduler scheduler=schedulerFactory.getScheduler();
        scheduler.start();
        Date scheduleDate=scheduler.scheduleJob(jobDetail,trigger);
        System.out.println("scheduled time is:"+ format.format(scheduleDate));
        Thread.sleep(2000);
        scheduler.shutdown();
        Thread.sleep(3000);
        scheduler.start();
    }
}
